package com.yjz.study.adapter.home

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.yjz.common.extend.loadUrl
import com.yjz.log.util.DisplayUtil
import com.yjz.study.R
import com.yjz.study.arouter.ARouterLogic
import com.yjz.study.http.entity.res.GoodsModel
import com.yjz.study.http.entity.res.selectPrice
import com.yjz.ui.recycler.item.HiDataItem
import com.yjz.ui.recycler.item.HiViewHolder

open class GoodsItem(private val goodsModel: GoodsModel, private val hotTab: Boolean) : HiDataItem<GoodsModel, HiViewHolder>(goodsModel) {

    val MAX_TAG_SIZE = 3
    override fun onBindData(holder: HiViewHolder, position: Int) {
        val context = holder.itemView.context
        val itemImage = holder.itemView.findViewById<ImageView>(R.id.item_image)
        val itemTitle = holder.itemView.findViewById<TextView>(R.id.item_title)
        val itemPrice = holder.itemView.findViewById<TextView>(R.id.item_price)
        val itemSaleDesc = holder.itemView.findViewById<TextView>(R.id.item_sale_desc)


        itemImage.loadUrl(goodsModel.sliderImage)
        itemTitle.text = goodsModel.goodsName

        itemPrice.text = selectPrice(goodsModel.groupPrice, goodsModel.marketPrice)
        itemSaleDesc.text = goodsModel.completedNumText

        if (!TextUtils.isEmpty(goodsModel.tags)) {
            val itemLabelContainer = holder.itemView.findViewById<LinearLayout>(R.id.item_label_container)
            itemLabelContainer.visibility = View.VISIBLE
            val split = goodsModel.tags.split(" ")
            for (index in split.indices) { //0...split.size-1
                //0  ---3
                val childCount = itemLabelContainer.childCount
                if (index > MAX_TAG_SIZE - 1) {
                    // 倒叙
                    for (index in childCount - 1 downTo MAX_TAG_SIZE - 1) {
                        // itemLabelContainer childcount =5
                        // 3，后面的两个都需要被删除
                        itemLabelContainer.removeViewAt(index)
                    }
                    break
                }
                //这里有个问题，有着一个服用的问题   5 ,4
                //解决上下滑动复用的问题--重复创建的问题
                val labelView: TextView = if (index > childCount - 1) {
                    val view = createLabelView(context, index != 0)
                    itemLabelContainer.addView(view)
                    view
                } else {
                    itemLabelContainer.getChildAt(index) as TextView
                }
                labelView.text = split[index]
            }
        }

        if (!hotTab) {
            val margin = DisplayUtil.dp2px(2f)
            val params = holder.itemView.layoutParams as RecyclerView.LayoutParams
            val parentLeft = hiAdapter?.getAttachRecyclerView()?.left ?: 0
            val parentPaddingLeft = hiAdapter?.getAttachRecyclerView()?.paddingLeft ?: 0
            val itemLeft = holder.itemView.left
            if (itemLeft == (parentLeft + parentPaddingLeft)) {
                params.rightMargin = margin
            } else {
                params.leftMargin = margin
            }
            holder.itemView.layoutParams = params
        }

        holder.itemView.setOnClickListener {
            val bundle = Bundle()
            bundle.putString("goodsId", goodsModel.goodsId)
            bundle.putSerializable("goodsModel", goodsModel)
            ARouterLogic.navigation(ARouterLogic.Path.GOOD_DETAIL, bundle)
        }
    }

    private fun createLabelView(context: Context, withLeftMargin: Boolean): TextView {
        val labelView = TextView(context)
        labelView.setTextColor(ContextCompat.getColor(context, com.yjz.ui.R.color.color_e75))
        labelView.setBackgroundResource(R.drawable.shape_goods_label)
        labelView.textSize = 11f
        labelView.gravity = Gravity.CENTER
        val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            DisplayUtil.dp2px(16f)
        )
        params.leftMargin = if (withLeftMargin) DisplayUtil.dp2px(5f) else 0
        labelView.layoutParams = params
        return labelView
    }

    override fun getItemLayoutRes(): Int {
        return if (hotTab) R.layout.layout_home_goods_list_item1 else R.layout.layout_home_goods_list_item2
    }

    override fun getSpanSize(): Int {
        return if (hotTab) super.getSpanSize() else 1
    }
}